package controller;

import moder.TbProduct;
import moder.TbUser;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/forward.do")
public class ForwardController extends HttpServlet {
    service.TbProductService productService = new service.impl.TbProductServiceImpl();
    service.TbUserService userService = new service.impl.TbUserServiceImpl();
    service.UserService userService2 = new service.impl.UserServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        if(("login").equals(type)) {
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
        if(("reg").equals(type)) {
            request.getRequestDispatcher("/register.jsp").forward(request, response);
        }
        if(("more").equals(type)) {
            String pid = request.getParameter("pid");
            TbProduct product = productService.getAllProduct(pid);
            request.setAttribute("product", product);
            String cidH = request.getParameter("cid");
            request.setAttribute("cidH", cidH);//cid
            String currpage = request.getParameter("currpage");
            request.setAttribute("currpage",currpage);//当前页
            request.getRequestDispatcher("/page.jsp").forward(request, response);
        }
        if(("myself").equals(type)) {
            String uid = request.getParameter("uid");
            TbUser vip = userService.getUserByUserId(uid);
            //System.out.println(vip.getUsername());
            request.setAttribute("vip", vip);
            request.getRequestDispatcher("/edituser.jsp").forward(request, response);
        }
        if(("info").equals(type)) {
            String uid =request.getParameter("uid");
            String uname = new String(request.getParameter("uname").getBytes("ISO-8859-1"),"utf-8");
            String utelephone = new String(request.getParameter("utel").getBytes("ISO-8859-1"),"utf-8");
            String ubirthday =  new String(request.getParameter("ubir").getBytes("ISO-8859-1"),"utf-8");
            String usex =  new String(request.getParameter("sex").getBytes("ISO-8859-1"),"utf-8");
            userService2.addInformation(uid, uname, utelephone, ubirthday, usex);
            request.getRequestDispatcher("/mymsg.jsp").forward(request, response);
        }
        if(("myselfAll").equals(type)) {
            request.getRequestDispatcher("/my-user.jsp").forward(request, response);
        }
        if(("mymsg").equals(type)) {
            String uid = request.getParameter("uid");
            TbUser user = userService.getUserByUserId(uid);
            request.setAttribute("user", user);
            request.getRequestDispatcher("/mymsg.jsp").forward(request, response);
        }
    }
}
